package com.seamtop.cuber.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.seamtop.cuber.server.service.CuberInitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by zongjunfeng on 2015/9/6.
 */
@Controller("CuberInitController")
@RequestMapping("cuber")
public class CuberInitController {

    private static final Logger LOG = LoggerFactory.getLogger(CuberInitController.class);


    @Autowired
    private CuberInitService cuberInitService;

    @RequestMapping("/init.action")
    public @ResponseBody
    JSONObject init(HttpServletRequest request, HttpServletResponse response,
                       ModelMap model) {
        LOG.info("------Cuber Init Start-----");
        cuberInitService.createTable();
        LOG.info("------Cuber Init End-----");
        return null;
    }

    @RequestMapping("/completeInit.action")
    public @ResponseBody
    JSONObject completeInit(HttpServletRequest request, HttpServletResponse response,
                       ModelMap model) {
        LOG.info("------Cuber Complete Init Start-----");
        cuberInitService.coverTable();
        LOG.info("------Cuber Complete Init End-----");
        return null;
    }

    @RequestMapping("/sysReload.action")
    public @ResponseBody
    void reloadConfInfo(HttpServletRequest request, HttpServletResponse response,
                               ModelMap model) {
        try {
            cuberInitService.sysReload();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
